package com.infore.gateway.main.utils;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class LocalDateUtils {

    /**
     * 年
     */
    public final static String DATE_YEAR="year";
    /**
     * 月
     */
    public final static String DATE_MONTH="month";
    /**
     * 日
     */
    public final static String DATE_DAY="day";
    /**
     * 季度
     */
    public final static String DATE_QUARTER="quarter";

    public static final String  DEFAULT_DATE_FORMATER = "yyyy-MM-dd HH:mm:ss";

    public static final String  DATE_FORMATER = "yyyy-MM-dd";

    public static final String  MONTH_FORMATER = "yyyy-MM";

    /**
     * 计算机元年
     */
    public static final String  COMPUTER_FIRST_YEAR = "1970-01-01";


    public static Date asDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }
 
    public static Date asDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }
 
    public static LocalDate asLocalDate(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
    }
 
    public static LocalDateTime asLocalDateTime(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static Date plusSeconds(Date date , Long seconds){
        return asDate(asLocalDateTime(date).plusSeconds(seconds));
    }

    public static Date plusDays(Date date , Long days){
        return asDate(asLocalDateTime(date).plusDays(days));
    }

    public static Date plusWeeks(Date date , Long weeks){
        return asDate(asLocalDateTime(date).plusWeeks(weeks));
    }

    public static Date plusMonths(Date date , Long months){
        return asDate(asLocalDateTime(date).plusMonths(months));
    }

    public static Date plusYears(Date date , Long years){
        return asDate(asLocalDateTime(date).plusYears(years));
    }

    public static Date plusHours(Date date , Long hours){
        return asDate(asLocalDateTime(date).plusHours(hours));
    }

    public static Date plusMinutes(Date date , Long minutes){
        return asDate(asLocalDateTime(date).plusMinutes(minutes));
    }

    public static Date minusDays(Date date , Long days){
        return asDate(asLocalDateTime(date).minusDays(days));
    }

    public static Date minusWeeks(Date date , Long weeks){
        return asDate(asLocalDateTime(date).minusWeeks(weeks));
    }

    public static Date minusYears(Date date , Long years){
        return asDate(asLocalDateTime(date).minusYears(years));
    }

    public static Date minusSeconds(Date date , Long seconds){
        return asDate(asLocalDateTime(date).minusSeconds(seconds));
    }

    public static Date minusHours(Date date , Long hours){
        return asDate(asLocalDateTime(date).minusHours(hours));
    }

    public static Date minusMinutes(Date date , Long minutes){
        return asDate(asLocalDateTime(date).minusMinutes(minutes));
    }

    public static Date minusMonths(Date date , Long months){
        return asDate(asLocalDateTime(date).minusMonths(months));
    }
    public static Long dayBeginLong(Date date ){
        String format = format(date, DATE_FORMATER) + " 00:00:00";
        return parseString(format).getTime();
    }


    public static Date parseString(String dateStr){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMATER);
        LocalDateTime localDateTime = LocalDateTime.parse(dateStr,formatter);
        return asDate(localDateTime);
    }

    public static LocalDateTime parseToLocalDateTime(String dateStr){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMATER);
        return LocalDateTime.parse(dateStr,formatter);
    }

    public static Date parseString(String dateStr,String pattern){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        LocalDateTime localDateTime = LocalDateTime.parse(dateStr,formatter);
        return asDate(localDateTime);
    }

    public static LocalDateTime parseToLocalDateTime(String dateStr,String pattern){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.parse(dateStr,formatter);
    }

    public static String format(Date date,String pattern){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        LocalDateTime localDateTime = asLocalDateTime(date);
        return localDateTime.format(formatter);
    }

    public static String format(Date date){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMATER);
        LocalDateTime localDateTime = asLocalDateTime(date);
        return localDateTime.format(formatter);
    }

}